package ${packageName}.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${packageName}.dto.${className}AddDTO;
import ${packageName}.utils.${className}Utils;
import ${packageName}.vo.${className}Vo;
import ${packageName}.dto.${className}ModDTO;
import ${packageName}.dto.${className}QueryDTO;
import ${packageName}.entity.${className};
import ${packageName}.mapper.${className}Mapper;
import ${packageName}.service.${className}Service;
import com.shoulder.boot.${code}.exception.${code?cap_first}Exception;
import com.shoulder.core.fetch.BaseIds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.Collections;
/**
* ${comment} 服务实现类
*
* @author ${author!'unKnown'}
* @since ${createTime}
*/
@Service
@Transactional
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}> implements ${className}Service {


    @Override
    public List<${className}Vo> listQuery(${className}QueryDTO queryDTO) {
        List<${className}> list = this.baseMapper.selectList(${className}Utils.buildQueryWrapper(queryDTO));
        if (CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        return ${className}Utils.buildUtils().toVos(list);
    }

    @Override
    public ${className}Vo look(String id) {
        ${className} entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new ${code?cap_first}Exception("${comment}不存在");
        }
        return ${className}Utils.buildUtils().toVo(entity);
    }

    @Override
    public List<${className}Vo> looks(BaseIds<String> baseIds) {
        List<${className}> list = this.baseMapper.selectBatchIds(baseIds.getIds());
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return ${className}Utils.buildUtils().toVos(list);
    }

    @Override
    public void del(String id) {
        ${className} entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new ${code?cap_first}Exception("${comment}不存在");
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void batchDel(BaseIds<String> baseIds) {
        this.baseMapper.deleteBatchIds(baseIds.getIds());
    }


    @Override
    public Page<${className}Vo> pageQuery(${className}QueryDTO queryDTO) {
        Page<${className}> selectPage = this.baseMapper.selectPage(new Page<>(queryDTO.getCurrent(), queryDTO.getSize())
            , ${className}Utils.buildQueryWrapper(queryDTO));
        if (selectPage == null || CollectionUtils.isEmpty(selectPage.getRecords())) {
            return new Page<>();
        }
        return ${className}Utils.buildUtils().toPage(selectPage);
    }

    @Override
    public void mod(${className}ModDTO modDTO) {
        ${className} info = Optional.ofNullable(this.baseMapper.selectById(modDTO.getId())).orElseThrow(() -> new ${code?cap_first}Exception("${comment}不存在"));
        BeanUtils.copyProperties(modDTO, info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void add(${className}AddDTO addDTO) {
        ${className} info = new ${className}();
        BeanUtils.copyProperties(addDTO, info);
        this.baseMapper.insert(info);
    }
}
